<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/21
  Time: 15:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户主页</title>
    <link rel="stylesheet" href="./css/index.css">
</head>
<body>
<%
    if (session.getAttribute("User") == null){
        response.sendRedirect("login.jsp");
    }
%>
<div class="container">
    <h1>登录成功！</h1>
    <p>账号：<span id="userid">${User.getUserId()}</span></p>
    <p>用户名：${User.getNickname()}</p>
</div>
<div class="container">
    <%--加入房间--%>
    <form action="./game/game.jsp" id="game" method="post">
        <label for="roomid">房间号：</label>
        <input type="text" name="roomid" id="roomid"> <br>
        <span id="msg" class="error-msg"></span> <br>
        <button type="button" id="confirm">确认</button>
    </form>
</div>
<%--排行榜--%>
<div class="container">
    <h1>排行榜</h1>
    <jsp:include page="score_top.jsp"></jsp:include>
</div>

<script type="text/javascript" src="./js/jquery-3.7.1.min.js"></script>
<script type="text/javascript" src="./js/index.js"></script>
<script>
    // 给确认按钮添加点击事件，并判断输入框是否为空
    $("#confirm").click(function (){
        // 判断会话是否超时超时就重定向
        <c:if test="${empty User}">
        window.location.href = "index.jsp";
        </c:if>
        var userData = "userid="+"${User.getUserId()}"+"&roomid="+$("#roomid").val();
        // console.log(userData);
        $.post("./trygame", userData, function(response){
            var jsonObj = JSON.parse(response);
            if(jsonObj.status == "success"){
                window.location.href = jsonObj.content;
            }else if(jsonObj.status == "fail"){
                $("#msg").text(jsonObj.content);
            }
        });
    })
</script>
</body>
</html>